package com.song.controller.user;

import com.song.constant.JwtClaimsConstant;
import com.song.dto.UserLoginDTO;
import com.song.entity.User;
import com.song.properties.JwtProperties;
import com.song.result.Result;
import com.song.service.UserService;
import com.song.utils.JwtUtil;
import com.song.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    @RequestMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("用户登录,登录参数：{}",userLoginDTO);
        User user = userService.userWxLogin(userLoginDTO);
        //生成JWT令牌
        Map<String,Object> claims = new HashMap();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.generateJwt(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),claims);
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build();
        return Result.success(userLoginVO,"登录成功");
    }
}
